/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.web.config;

import yhao.infra.web.ConfigFactory;
import yhao.infra.web.filter.RedisSessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * redis Session实现配置类
 *
 * @author yoara
 */
public class RedisSessionConfig {
    @Autowired
    private Environment env;

    @Bean
    public FilterRegistrationBean redisSessionFilterBean() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new RedisSessionFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("sessionId",env.getProperty(ConfigFactory.REDIS_SESSION_FILTER_SESSION_ID,"sid"));
        registration.addInitParameter("cookieDomain",env.getProperty(ConfigFactory.REDIS_SESSION_FILTER_COOKIE_DOMAIN,""));
        registration.addInitParameter("cookiePath",env.getProperty(ConfigFactory.REDIS_SESSION_FILTER_COOKIE_PATH,"/"));
        registration.addInitParameter("sessionIdHeaderKey",env.getProperty(ConfigFactory.REDIS_SESSION_FILTER_SESSION_ID_HEADER_KEY,""));
        registration.addInitParameter("sessionSourceHeaderKey",env.getProperty(ConfigFactory.REDIS_SESSION_FILTER_SESSION_SOURCE_HEADER_KEY,""));
        return registration;
    }
}
